package cn.edu.buu.news.controller;

import cn.edu.buu.news.entity.Case;
import cn.edu.buu.news.service.CaseService;
import cn.edu.buu.news.vo.PageQuery;
import cn.edu.buu.news.vo.PageResult;
import cn.edu.buu.news.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private CaseService caseService;  // 改为CaseService

    // 新增案例
    @PostMapping
    public Result add(@RequestBody Case newsCase) {  // 参数类型改为Case
        caseService.add(newsCase);
        return Result.success();
    }

    // 删除案例
    @DeleteMapping("/{caseId}")  // 统一参数名为caseId
    public Result delete(@PathVariable Long caseId) {
        caseService.deleteById(caseId);
        return Result.success();
    }

    // 更新案例
    @PutMapping
    public Result update(@RequestBody Case newsCase) {  // 保持参数类型为Case
        caseService.update(newsCase);
        return Result.success();
    }

    // 根据ID查询案例
    @GetMapping("/{caseId}")  // 统一参数名为caseId
    public Result<Case> getById(@PathVariable Long caseId) {  // 返回类型改为Case
        return Result.success(caseService.findById(caseId));
    }

    // 查询所有案例
    @GetMapping
    public Result<List<Case>> getAll() {  // 返回类型改为List<Case>
        return Result.success(caseService.findAll());
    }

    // 分页查询案例
    @PostMapping("/list")
    public Result<PageResult<Case>> getByPage(@RequestBody PageQuery query) {  // 分页结果改为Case
        return Result.success(caseService.findByPage(query));
    }
}
